using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Configuration.Provider;


namespace GetSharePointToValidate.Controls
{
    public class GSPTV_SitemapPath : Control
    {
        private SiteMapProvider siteMapProvider;
        private List<SiteMapNode> siteMapNodes;

        private string cssWrapperClassName;

        public string CssWrapperClassName
        {
            get
            {
                if (string.IsNullOrEmpty(cssWrapperClassName))
                    return string.Empty;
                else
                    return cssWrapperClassName;
            }
            set { cssWrapperClassName = value; }
        }

        private string csssitePathCurrent = "sitePathCurrent";
        public string CssSitePathCurrent
        {
            get { return csssitePathCurrent; }
            set { csssitePathCurrent = value; }
        }

        private string csssitePathRootNode = "sitePathRootNode";
        public string CsssitePathRootNode
        {
            get { return csssitePathRootNode; }
            set { csssitePathRootNode = value; }
        }

        private bool showRootNode = true;

        public bool ShowRootNode
        {
            get
            {
                return showRootNode;
            }
            set { showRootNode = value; }
        }

        private bool showLinkInCurrentNode = false;
        public bool ShowLinkInCurrentNode
        {
            get { return showLinkInCurrentNode; }
            set { showLinkInCurrentNode = value; }
        }

        private string pathSeparator = " > ";
        public string PathSeparator
        {
            get { return pathSeparator; }
            set { pathSeparator = value; }
        }

        private string siteMapProviderName;

        public string SiteMapProviderName
        {
            get { return siteMapProviderName; }
            set { siteMapProviderName = value; }
        }

        public GSPTV_SitemapPath()
        {
            siteMapNodes = new List<SiteMapNode>();
        }
        
        protected override void Render(HtmlTextWriter writer)
        {
            try
            {
                if (SiteMapProviderName == null || SiteMapProviderName.Length == 0)
                    siteMapProvider = SiteMap.Provider.RootProvider;
                else
                    siteMapProvider = SiteMap.Providers[SiteMapProviderName];

                buildSiteMap(siteMapProvider.CurrentNode);

                
                if(!string.IsNullOrEmpty(CssWrapperClassName))
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, CssWrapperClassName);

                writer.RenderBeginTag(HtmlTextWriterTag.Ul);
                for (int i = (siteMapNodes.Count - 1); i >= 0; i--)
                {
                    SiteMapNode node = siteMapNodes[i];

                    if (node.Equals(siteMapProvider.RootNode))
                        writer.AddAttribute(HtmlTextWriterAttribute.Class, CsssitePathRootNode);

                    if (i == 0)
                        writer.AddAttribute(HtmlTextWriterAttribute.Class, CssSitePathCurrent);

                    writer.RenderBeginTag(HtmlTextWriterTag.Li);
                    writer.AddAttribute(HtmlTextWriterAttribute.Href, node.Url);
                    writer.AddAttribute(HtmlTextWriterAttribute.Title, node.Description);

                    if ((ShowLinkInCurrentNode && i != 0) || i != 0)
                    {
                        writer.RenderBeginTag(HtmlTextWriterTag.A);
                        writer.Write(node.Title);
                        writer.RenderEndTag();
                        
                    }
                    else
                        writer.Write(node.Title);
             
                    if (i > 0)
                        writer.WriteLine(PathSeparator);

                    writer.RenderEndTag();
                    
                }
                writer.RenderEndTag();

            }
            catch (Exception ex)
            {
                writer.Write(ex.ToString());
            }
            base.Render(writer);
        }
        public void buildSiteMap(SiteMapNode Node)
        {
            if(!Node.RootNode.Equals(Node))
                siteMapNodes.Add(Node);
            else if (Node.RootNode.Equals(Node) && ShowRootNode)
                siteMapNodes.Add(Node);

            if(Node.ParentNode != null)
                buildSiteMap(Node.ParentNode);
        }
    }
}
